package cn.id68.www.http.request;

import android.text.TextUtils;

import java.util.Map;

import cn.id68.www.http.OkHttpUtils;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/3/21.
 */

public class OrtherRequest extends OkHttpRequest {

    private static MediaType MEDIA_TYPE_PLAIN = MediaType.parse("text/plain;charset=utf-8");

    private RequestBody requestBody;
    private String method;
    private String content;

    public OrtherRequest(String url, Object tag, Map<String, String> params, Map<String, String> headers, int id, RequestBody requestBody, String method, String content) {
        super(url, tag, params, headers, id);
        this.requestBody = requestBody;
        this.method = method;
        this.content = content;
    }

    @Override
    protected RequestBody buildRequestBody() {
        if (requestBody == null){
            throw  new NullPointerException("body 不能空");
        }

        if (requestBody == null && !TextUtils.isEmpty(content)){
            requestBody  = RequestBody.create(MEDIA_TYPE_PLAIN,content);
        }

        return requestBody;
    }

    @Override
    protected Request buildRequest(RequestBody requestBody) {
       if (method.equals(OkHttpUtils.METHOD.PUT)){
           builder.put(requestBody);
       }else if (method.equals(OkHttpUtils.METHOD.DELETE)){
           if (requestBody == null){
               builder.delete();
           }else {
               builder.delete(requestBody);
           }
       }else if (method.equals(OkHttpUtils.METHOD.HEAD)){
           builder.head();
       }else if (method.equals(OkHttpUtils.METHOD.PATCH)){
           builder.patch(requestBody);
       }
        return builder.build();
    }
}
